home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 February / CMCD0205.ISO / Software / Freeware / Programare / bluej / bluejsetup-203.exe / {app} / examples / file-reader / FileReader.java < prev    next >
Text File  |  2004-12-19  |  3KB  |  90 lines

  1. import java.io.*;
  2. import java.net.URL;
  3.  
  4. /**
  5.  * This is a little demo showing how to read text files. It will find files
  6.  * that are situated anywhere in the classpath.
  7.  *
  8.  * Currently, two demo methods are available. Both simply print a text file to the
  9.  * terminal. One returns exceptions in case of a problem, the other prints out
  10.  * error messages.
  11.  * 
  12.  * @author Michael KÜlling
  13.  * @version 1.0 (19. Feb 2002)
  14.  */
  15. public class FileReader
  16. {
  17.     /**
  18.      * Create a file reader
  19.      */
  20.     public FileReader()
  21.     {
  22.         // nothing to do...
  23.     }
  24.  
  25.     /**
  26.      * Show the contents of the file 'fileName' on standard out (the text terminal).
  27.      *
  28.      * @param fileName  The name of the file to show
  29.      * @throws  IOException if the file could not be opened
  30.      */
  31.     public void showFile(String fileName)
  32.         throws IOException
  33.     {
  34.         InputStream fstream = openFile(fileName);
  35.  
  36.         // wrap the stream into an InputStreamReader, so that we read characters
  37.         // rather than bytes (important for non-ascii characters); then wrap it into
  38.         // a BufferedReader, so that we can read lines, rather than single characters
  39.  
  40.         BufferedReader in = new BufferedReader(new InputStreamReader(fstream));
  41.  
  42.         // okay, we're ready to go...
  43.  
  44.         System.out.println("File: " + fileName);
  45.         String line = in.readLine();
  46.         while(line != null) {
  47.             System.out.println(line);
  48.             line = in.readLine();
  49.         }
  50.         System.out.println("<end of file>");
  51.     }
  52.         
  53.     /**
  54.      * Same as 'showfile', but don't throw exceptions. If an error occurs,
  55.      * write an error message to the terminal.
  56.      *
  57.      * @param fileName  The name of the file to show
  58.      */
  59.     public void checkedShowFile(String fileName)
  60.     {
  61.         try {
  62.             showFile(fileName);
  63.         }
  64.         catch(IOException exc) {
  65.             System.out.println("There was a problem showing this file.");
  66.             System.out.println("The error encountered is:");
  67.             System.out.println(exc);
  68.         }
  69.     }
  70.  
  71.     /**
  72.      * Open a text file and return a stream to read from that file.
  73.      * The file can reside anywhere in the classpath.
  74.      *
  75.      * @param fileName  The name of the file to open
  76.      * @return An open stream to read from the file
  77.      * @throws  IOException if the file could not be opened
  78.      */
  79.     public InputStream openFile(String fileName)
  80.         throws IOException
  81.     {
  82.         if(fileName == null)
  83.             throw new IOException("Cannot open file - filename was null.");
  84.         URL url = getClass().getClassLoader().getResource(fileName);
  85.         if(url == null)
  86.             throw new IOException("File not found: " + fileName);
  87.         return url.openStream();
  88.     }
  89. }
  90.